package com.farmpost.server.controller;


import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.pojo.UserLogin;
import com.farmpost.server.service.IUserInfoService;
import com.farmpost.server.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * <p>
 * 专用用户登录 前端控制器
 * </p>
 *
 * @author king
 * @since 2022-04-03
 */
@Api(tags = "UserLoginController")
@RestController

public class UserLoginController {

    @Autowired
    private IUserInfoService iUserInfoService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody UserLogin userLogin, HttpServletRequest request) {
        return iUserInfoService.login(userLogin.getUsername(), userLogin.getUserPassword(),userLogin.getCode(), request);
    }


    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/user/info")
    public UserInfo getUserInfo(Principal principal) {
        if (principal == null)
            return null;
        String username = principal.getName();
        UserInfo userInfo = iUserInfoService.getUserInfoByUserName(username);
        userInfo.setUserPassword(null);
        return userInfo;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout() {
        return RespBean.success("退出登录成功");
    }

    @ApiOperation(value = "账号注销")
    @PostMapping("/quit")
    public RespBean quitSys() {
        return iUserInfoService.quitSys();
    }
}
